require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

var myFun = {};
require(['jquery', 'doT', 'datetimepicker', 'select2', 'common', 'bootstrap'], function($, doT, datetimepicker, select2, common) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var school_id = sessionStorage.getItem('school_id');
        var page_size = 20;
        $('#userInfo').hide();

        function initDatetimepicker() {
            var date = new Date();
            date = new Date(date - 24 * 60 * 60 * 1000);
            var date_string = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            $('.user_date').datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                maxView: 'decade',
                minView: 'month',
                endDate: date,
                initialDate: date
            });
            $('.user_time').datetimepicker({
                format: 'hh:ii:ss',
                autoclose: true,
                startView: 1,
                maxView: 'day',
                minView: 'hour',
                minuteStep: 1,
                title: date_string
            });

            $('#userdateStart').val(date_string);
            $('#userdateEnd').val(date_string);
            $('#timeStart').val('00:00:00');
            $('#timeEnd').val('23:59:59');

        }
        initDatetimepicker();



        //初始化学校
        function initSchool() {
            $.get(common.urlBase + "/school/allSchool.json", function(data, status) {
                data = eval('(' + data + ')');
                var all = { "id": "-1", "name": "全部" };
                var schools = data.data;
                schools.unshift(all);
                var schoolTmpl = doT.template($('#template_school').text());
                $('#querySchool').html(schoolTmpl(schools));
                $("#querySchool").select2();
                if (role != 1) {
                    $('#querySchool').attr('disabled', true);
                    $('#querySchool').val(school_id);
                    $('#select2-querySchool-container').text($('#querySchool option[value=' + school_id + ']').text());
                }
            });
        }
        initSchool();

        myFun.chooseInfo = function chooseInfo(obj) {
            $('#buyer').html($(obj).text() + '<span class="caret"></span>');
        }


        $('#userForm').submit(function() {
            var school_id = $('#querySchool').val();
            var start_time = Date.parse($('#userdateStart').val() + ' ' + $('#timeStart').val());
            var end_time = Date.parse($('#userdateEnd').val() + ' ' + $('#timeEnd').val());
            var info_choice = $('#buyer').text();
            var info = $('#buyerInfo').val();

            if (start_time >= end_time) {
                $('#tips').text('请选择正确的时间！');
                return false;
            } else {
                $('#tips').text('')
            }

            if (info_choice == '收货人姓名') {
                var nick = info;
            } else if (info_choice == '收货人电话') {
                var phone = info;
            }

            myFun.data = {
                'nick': nick,
                'phone': phone,
                'start_time': start_time,
                'end_time': end_time
            };

            refreshUser(1);

            return false;
        });

        function refreshUser(page) {
            getUser(page, page_size);
        }

        function getUser(page, page_size) {
            myFun.data.page = page;
            myFun.data.page_size = page_size;
            var userTmpl = doT.template($('#template_user').text());

            $.get(common.urlBase + '/user/search/get.json', myFun.data, function(data, sattu) {
                data = eval('(' + data + ')');
                if (data.data.total) {
                    var total = data.data.total;
                    $('#users').html(userTmpl(data.data.obj));
                    $('#total').text(total);
                    var pageNumber = Math.ceil(total / page_size);
                    initPaginator(page, 8, pageNumber);
                    $('#userInfo').show();
                } else {
                    $('#noticeMsg').text('无相关信息！');
                    $('#noticeModal').modal('show');
                    $('#userInfo').hide();
                    return false;
                }
            });

        }
        //初始化分页组件
        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function(event, oldPage, newPage) {
                    refreshUser(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }
    });
});
